$(function() {
	var sn = $(".inHouse_menu .menu_left #sn");
	var client_name = $(".inHouse_menu .menu_left #client_name");
	var introduction = $(".inHouse_menu .menu_left #introduction");

	getFocus(client_name);

	var eleValue = new Array();
	sn.bind('input propertychange', function() {
		if(sn.val().length == 4) {
			var str = '<p>SN:<span class="add_sn">' + sn.val() + '</span>&nbsp;<b style="color:limegreen">添加成功！</b></p>';
			$(".menu_right .sn_list").append(str);
			sn.val("");
		}
	});

	//获得焦点事件
	function getFocus(item) {
		item.focus();
	}

	$(".inHouse_menu .save #sub").click(function() {
		var c_name = client_name.val();
		var intro = introduction.val();
		var arrSn = new Array();
		$(".add_sn").each(function() {
			arrSn.push($(this).text());
		});
		var sns = arrSn;
		if(c_name == null || intro == null || sns.length == 0) {
			meg_append("商户名或商户信息或sn不能有空！","red");
		} else {
			var data = {
				name: c_name,
				introduction: intro,
				SNs: sns
			}

			$.ajax({
				url: "./addMerchants",
				type: "POST",
				data: JSON.stringify(data), // 转JSON字符串
				dataType: 'json',
				contentType: 'application/json', // contentType很重要
				success: function(result) {
					if(result.status == 200) {
						var back_info = "商户名："+c_name+"管理员账号："+result.account+"密码："+result.password;
						meg_append(back_info,"#00FFCC");
					}else{
						meg_append(result.message,"red");
					}
				}
			});
		}

	});
	$(".inHouse_menu .save #clear_all").click(function() {
		client_name.val("");
		introduction.val("");
		$(".menu_right .sn_list").empty();
		$(".inHouse_menu .meg").empty();
		getFocus(client_name);
	});

	function meg_append(meg, cor) {
		var str = '<strong>提示：</strong><label style="color: ' + cor + ';">' + meg + '</label>';
		$(".inHouse_menu .meg").empty();
		$(".inHouse_menu .meg").append(str);
	}
});